package com.sugon.controller.device;

import cn.hutool.core.lang.Assert;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.DevLineDevCoordinateEntity;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.entity.SysDeptEntity;
import com.sugon.service.DevLineDevCoordinateService;
import com.sugon.service.DeviceInfoService;
import com.sugon.service.SysDeptService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 产线中设备坐标信息Controller
 *
 * @author sugon
 * @date 2020-05-25 16:06:02
 */
@Controller
@RequestMapping("devlinedevcoordinate")
public class DevLineDevCoordinateController {
    @Autowired
    private DevLineDevCoordinateService devLineDevCoordinateService;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<DevLineDevCoordinateEntity> coordinateList = devLineDevCoordinateService.queryList(query);

        int total = devLineDevCoordinateService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(coordinateList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 获取产线下设备列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/lineDevList")
    @ResponseBody
    public R lineDevList(@RequestParam Map<String, Object> params) {
        List<DevLineDevCoordinateEntity> coordinateList = devLineDevCoordinateService.queryList(params);
        return R.ok().put("list", coordinateList);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info")
    @ResponseBody
    public R info(@RequestParam String lineCode) {
        DevLineDevCoordinateEntity devLineDevCoordinate = devLineDevCoordinateService.getByCode(lineCode);
        return R.ok().put("lineCoorSet", devLineDevCoordinate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody DevLineDevCoordinateEntity devLineDevCoordinate) {
        if (devLineDevCoordinateService.getByCode(devLineDevCoordinate.getCode()) != null) {
            return R.error("产线配置已存在，请确认!");
        }
        // 设置设备名称
        String lineDeptId = devLineDevCoordinate.getCode();
        SysDeptEntity lineInfo = deptService.queryObject(lineDeptId);
        Assert.notNull(lineInfo,"产线[{0}]不存在,请确认",lineDeptId);
        devLineDevCoordinate.setName(lineInfo.getDcode());
        devLineDevCoordinateService.save(devLineDevCoordinate);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody DevLineDevCoordinateEntity devLineDevCoordinate) {
        if (devLineDevCoordinateService.getByCode(devLineDevCoordinate.getCode()) == null) {
            return R.error("产线配置不存在，请确认!");
        }
        devLineDevCoordinateService.update(devLineDevCoordinate);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/dev/delete")
    @ResponseBody
    public R delete(@RequestParam String id) {
        devLineDevCoordinateService.delete(id);
        return R.ok();
    }

    @NonBusinessParamValid
    @RequestMapping("/dev/save")
    @ResponseBody
    public R save2(@Validated @RequestBody DevLineDevCoordinateEntity devLineDevCoordinate, BindingResult bindingResult) {
        // 校验设备是否存在
        String devCode = devLineDevCoordinate.getCode();
        DeviceInfoEntity device = deviceInfoService.getInfoByDevCode(devCode);
        if(device == null){
            return R.error("设备不存在");
        }
        // 校验是否已关联
//        DevLineDevCoordinateEntity devCoordInDB = devLineDevCoordinateService.getByCode(devCode);
//        if(devCoordInDB != null){
//            return R.error("该设备已关联");
//        }
        devLineDevCoordinate.setName(device.getDevName());
        devLineDevCoordinateService.save(devLineDevCoordinate);
        return R.ok();
    }


    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevLineDevCoordinateEntity> list = devLineDevCoordinateService.queryList(params);

        return R.ok().put("list", list);
    }
}
